<?php
header('Content-type: text/html; charset=utf-8');
$x = $_POST["x"];
$y = $_POST["y"];
$w = $_POST["w"];
$h = $_POST["h"];
$text = $_POST["text"];
$page = $_POST["page"];
$boxiff = $_POST["boxiff"];
$meow = $_POST["meow"];
$time=time();
$logfile="log.log";
//echo "=$x =$y =$w =$h =$text =$page =$boxiff =$meow =$time";
switch($meow){
case 'new':
	//echo is_writeable($tmp)?'T':'F';echo is_readable($tmp)?'T':'F';echo file_exists($tmp)?'T':'F';
	$cp = fopen($logfile, "a+") or die('failed');// 讀寫模式, 指標於最後, 找不到會嘗試建立檔案
	//echo @chmod($tmp,0666)?'c':'F';
	rewind($cp); //從頭讀取
	$buf=fread($cp,1000000);
	ftruncate($cp, 0);//砍到零
	$text = str_replace(",","&#44;",$text);
	$new_buf=''.$page.','.$time.','.$x.','.$y.','.$w.','.$h.','.$boxiff.','.$text.'';
	$buf=$new_buf."\n".$buf;
	$cellarr=array();
	$cellarr=explode("\n",$buf);
	//array_splice($cellarr,300);//移除陣列第300項之後的部份
	$buf=implode("\n",$cellarr);
	fputs($cp, $buf);
	fclose($cp);
	//echo "]".filesize($tmp);//log檔案大小
break;
case 'edit':
	//echo is_writeable($tmp)?'T':'F';echo is_readable($tmp)?'T':'F';echo file_exists($tmp)?'T':'F';
	$cp = fopen($logfile, "a+") or die('failed');// 讀寫模式, 指標於最後, 找不到會嘗試建立檔案
	//echo @chmod($tmp,0666)?'c':'F';
	rewind($cp); //從頭讀取
	$buf=fread($cp,1000000);
	ftruncate($cp, 0);//砍到零
	$text = str_replace(",","&#44;",$text);
	$cellarr=array();
	$cellarr=explode("\n",$buf);
	$countline = count($cellarr);
	for($i = 0; $i < $countline; $i++){
		$cellarr[$i]=explode(",",$cellarr[$i]);
		if($cellarr[$i][1]==$boxiff){
			$cellarr[$i][2]=$x;
			$cellarr[$i][3]=$y;
			$cellarr[$i][4]=$w;
			$cellarr[$i][5]=$h;
			$cellarr[$i][7]=$text;
			
		}
		$cellarr[$i]=implode(",",$cellarr[$i]);
	}
	$buf=implode("\n",$cellarr);
	fputs($cp, $buf);
	fclose($cp);
break;
case 'delx':
	//echo is_writeable($tmp)?'T':'F';echo is_readable($tmp)?'T':'F';echo file_exists($tmp)?'T':'F';
	$cp = fopen($logfile, "a+") or die('failed');// 讀寫模式, 指標於最後, 找不到會嘗試建立檔案
	//echo @chmod($tmp,0666)?'c':'F';
	rewind($cp); //從頭讀取
	$buf=fread($cp,1000000);
	ftruncate($cp, 0);//砍到零
	$text = str_replace(",","&#44;",$text);
	$cellarr=array();
	$cellarr=explode("\n",$buf);
	$countline = count($cellarr);
	for($i = 0; $i < $countline; $i++){
		$chk=0;
		$cellarr[$i]=explode(",",$cellarr[$i]);
		if($cellarr[$i][1]==$boxiff){
			$chk=1;
		}
		$cellarr[$i]=implode(",",$cellarr[$i]);
		if($chk){unset($cellarr[$i]);}
	}
	$buf=implode("\n",$cellarr);
	fputs($cp, $buf);
	fclose($cp);
break;
}
echo "]".filesize($logfile);//log檔案大小

echo "<html><head><META HTTP-EQUIV=\"refresh\" content=\"1;URL=./?p=".$page."\">
<META HTTP-EQUIV='Content-type' CONTENT='text/html; charset=utf-8'>
</head><body></body></html>";
exit("EXIT 画面を切り替えます");
?>
